import datetime
from django import template

__author__ = 'Sharp'

register = template.Library()

@register.filter
def time_left(goal):
    years = goal.due_date.year - datetime.date.today().year
    if years:
        return "%s years away" % years
    months = goal.due_date.month - datetime.date.today().month
    if months:
        return "%s months away" % months
    days = goal.due_date.day - datetime.date.today().day
    if days:
        return "%s days away" % days

@register.filter
def percentage(goal):
    all_task = len(goal.task_set.all())
    completed_tasks = len(goal.completed_task_set.all())
    for sub_goal in goal.goal_set.all():
        all_task += len(sub_goal.task_set.all())
        completed_tasks += len(sub_goal.completed_task_set.all())
    if all_task == 0:
        return r'0%'
    return '{:.0%}'.format((float(completed_tasks) / all_task))